﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using KeMei.Common;

public partial class Manager_lm_Edit : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["UserInfo"] == null)
        {
            Response.Redirect(ConfigHelper.SystemUrl + "Manager/login.aspx", true);

        }
        if (!IsPostBack)
        {
            bindParentList();

            string ID = Request["ID"];
            if (!string.IsNullOrEmpty(ID))
            {
                BindModel(int.Parse(KeMei.Common.PageValidate.InputText(ID, 10)));
            }
        }
    }

    protected void BindModel(int ID)
    {
        KeMei.Model.Syslm model = null;

        KeMei.BLL.Syslm bll = new KeMei.BLL.Syslm();

        try
        {
            model = bll.GetModel(ID);

            if (model != null)
            {
                txtName.Text = model.TName;
                ddlParent.SelectedValue = model.ParentID.ToString();

            }
        }
        catch (Exception ex)
        {

            ExtAspNet.Alert.Show("绑定实体类失败:"+ex.Message);
        }
    }

    protected void bindParentList()
    {
        List<KeMei.Model.Syslm> list = null;
        KeMei.BLL.Syslm bll = new KeMei.BLL.Syslm();

        try
        {
            list = bll.GetModelList(" parentId=0 and lmType in(1,3) ");

            ddlParent.DataSource = list;
            ddlParent.DataTextField = "TName";
            ddlParent.DataValueField = "ID";
            ddlParent.DataBind();
        }
        catch (Exception ex)
        {

            ExtAspNet.Alert.Show("绑定所属栏目失败：" + ex.Message);
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        KeMei.Model.Syslm model = new KeMei.Model.Syslm();
        KeMei.BLL.Syslm bll = new KeMei.BLL.Syslm();
        model.TName = txtName.Text;
        model.ParentID = int.Parse(ddlParent.SelectedValue);
        model.lmType = 0;

        try
        {
            if (Request["ID"] == null)
            {
                bll.Add(model);
            }
            else
            {

                model.ID = int.Parse(Request["ID"]);

                bll.Update(model);
            }

            ExtAspNet.Alert.Show("保存成功！", String.Empty, ExtAspNet.Alert.DefaultIcon, ExtAspNet.ActiveWindow.GetHidePostBackReference());
        }
        catch (Exception ex)
        {

            ExtAspNet.Alert.Show("保存失败：" + ex.Message);
        }
    }
}